package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallNewsMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LitemallNewsService {

    @Autowired
    @Resource
    LitemallNewsMapper litemallNewsMapper;

    /**
     * 查看所有新闻
     *
     * @return
     */
    public Map<String, Object> selectallnews(Integer page, Integer limit, String sort, String order) {
        Map<String, Object> newsMap = new HashMap<>();


        int currPage = (page - 1) * limit;
        newsMap.put("list", litemallNewsMapper.selectallnews(currPage, limit, sort, order));

        Integer total = litemallNewsMapper.total();
        //59  / 20  == 2.xx  % 10 !=0
        int pages = (total + limit - 1) / limit;
        newsMap.put("pages", pages);
        newsMap.put("total", total);
        newsMap.put("limit", limit);
        newsMap.put("page", page);

        return newsMap;
    }

    /**
     * 修改新闻
     *
     * @param
     * @return
     */
    public int updatenewsByid(LitemallNews litemallNews) {

        return litemallNewsMapper.updatenewsByid(litemallNews);
    }

    /**
     * 添加新闻
     *
     * @param litemallNews
     * @return
     */
    public int insertnews(LitemallNews litemallNews) {

        return litemallNewsMapper.insertnews(litemallNews);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    public LitemallNews selectByid(int id) {

        return litemallNewsMapper.selectByid(id);

    }

    /**
     * 用于 添加浏览 ，点赞 评论 ，转发 ， 和删除
     *
     * @param litemallNews
     * @return
     */
    public int updateshujv(LitemallNews litemallNews) {

        return litemallNewsMapper.updateshujv(litemallNews);
    }

    public Object page(int page, int limit) {

        return null;
    }

    public void save(LitemallNews news) {
        litemallNewsMapper.insertnews(news);

    }
}
